from pathlib import Path
import csv
from datetime import datetime

import matplotlib.pyplot as plt

path = Path('sitka_weather_2021_simple.csv')
lines = path.read_text().splitlines()

reader = csv.reader(lines)
header_row = next(reader)

# 提取日期、最高温度、最低温度
dates, highs, lows = [], [], []
for row in reader:
    current_date = datetime.strptime(row[2],'%Y-%m-%d')
    high = int(row[4])
    low = int(row[5])
    dates.append(current_date)
    highs.append(high)
    lows.append(low)

# 根据数据绘图
plt.style.use('classic')
fig, ax = plt.subplots()
ax.plot(dates, highs, color='red',alpha=0.5)
ax.plot(dates, lows, color='blue',alpha=0.5)
ax.fill_between(dates, highs, lows, facecolor='Blue', alpha=0.1)

# 设置绘图的格式
ax.set_title("Daily High and Low Temperatures, 2021",fontsize=20)
ax.set_xlabel('Day',fontsize=16)
# 调用fig.autofmt_xdate()来绘制倾斜的日期标签，以免它们彼此重叠
fig.autofmt_xdate()
ax.set_ylabel("Temperature(F)",fontsize=16)
# 设置 y 轴数据值范围
ax.set_ylim(bottom=0, top=150)  # 例如，设置 y 轴范围为 0°F 至 120°F
# 设置刻度标记的样式
ax.tick_params(labelsize=16)

plt.show()